	--
-- Created by IntelliJ IDEA.
-- User: 刘祥
-- Date: 14-10-14
-- Time: 下午3:50
-- To change this template use File | Settings | File Templates.
--
require "src/util/serial"

LOG_LEVEL_INFO = 1
LOG_LEVEL_DEBUG = 2
LOG_LEVEL_WARNING = 3
LOG_LEVEL_ERROR = 4
LOG_LEVEL_FATAL = 5

LOG_OUTPUT_NONE = 0
LOG_OUTPUT_CONSOLE = 1
LOG_OUTPUT_FILE = 2
LOG_OUTPUT_ALL = 3

if DEBUG_VERSION then
    LOG_LEVEL = LOG_LEVEL_DEBUG
else
    LOG_LEVEL = LOG_LEVEL_WARNING
end
LOG_OUTPUT = LOG_OUTPUT_CONSOLE
LOG_TAGS = {}

local logName
local getLogPath = function()
        local path = cc.FileUtils:getInstance():getWritablePath()

        return path
end

local getLogName = function()
    if not logName then
        local cur_date = os.date("%Y-%m-%d",os.time())
        local path = getLogPath()
        logName = path..string.format("log_%s.txt",cur_date)
    end

    return logName
end


local myMatch = function(math_item,tag)
    if type(math_item)~= type(tag)then
        return
    end

    if type(math_item)== "number" then
        return math_item == tag
    end

    if type(math_item)== "string" then
        local lastChar_ = string.sub(math_item,-1,2)

        if lastChar_ == "*" then
            local newStr_ = string.sub(math_item,1,string.len(math_item)- 1)
            local begin_ = string.find(tag,newStr_,1)

            if begin_ == 1 then  --开头
                return true
            end
        else
            return math_item == tag
        end
    end
end

local writeLog = function(level,tag,...)
    if level < LOG_LEVEL then
        return
    end

    if LOG_OUTPUT == LOG_OUTPUT_NONE then
        return
    end

    if not tag then
        if 0 ~= #LOG_TAGS then
            return
        end
    end

    if tag and #LOG_TAGS > 0 then
        local match = false
        for _,match_item in pairs(LOG_TAGS)do
            if myMatch(match_item,tag)then
                match = true
                break
            end
        end

        if not match then
            return
        end
    end

    local msg = string.format(...)
    local level_str={
        [LOG_LEVEL_INFO] = "INFO",
        [LOG_LEVEL_DEBUG] = "DEBUG",
        [LOG_LEVEL_WARNING] = "WARNING",
        [LOG_LEVEL_ERROR] = "ERROR",
        [LOG_LEVEL_FATAL] = "FATAL",
    }

    if LOG_OUTPUT_ALL == LOG_OUTPUT or LOG_OUTPUT_CONSOLE == LOG_OUTPUT then
        if tag then
            print(string.format("[%5s]:%s-%s",level_str[level],tag,msg))
        else
            print(string.format("[%5s]:%s",level_str[level],msg))
        end
    end

    if LOG_OUTPUT_ALL == LOG_OUTPUT or LOG_OUTPUT_FILE == LOG_OUTPUT then
        local print_message
        local cur_dt = os.date("%Y-%m-%d %H:%M:%S",os.time())

        if tag then
            print_message = string.format("[%s][%5s]:%s-%s",cur_dt,level_str[level],tag,msg)
        else
            print_message = string.format("[%s][%5s]:%s",cur_dt,level_str[level],msg)
        end

        local handle = io.open(getLogName(),"a")
        if handle ~= nil then
            handle:write(print_message,"\n")
            handle:close()
        end
    end
end

local _info = function(tag,...)
    writeLog(LOG_LEVEL_INFO,tag,...)
end

local _warning = function(tag,...)
    writeLog(LOG_LEVEL_WARNING,tag,...)
end

local _debug = function(tag,...)
    writeLog(LOG_LEVEL_DEBUG,tag,...)
end

local _error = function(tag,...)
    writeLog(LOG_LEVEL_ERROR,tag,...)
    print(debug.traceback())
end

local _fatal = function(tag,...)
    writeLog(LOG_LEVEL_FATAL,tag,...)
    print(debug.traceback())
end

log = {
    info = _info,
    warning = _warning,
    debug = _debug,
    error = _error,
    fatal = _fatal,
}


